package com.example.arithmeticleetcode.learnArithmetic.avl;


import java.util.Comparator;

/**
 * @program: arithmetic-leetcode
 * @description:
 * @author: FangZhen
 * @create: 2020-08-25 16:52
 **/
public class BST<E> extends BinaryTree<E> {

    private Comparator<E> comparator;

    public BST() {
        this(null);
    }

    public BST(Comparator<E> comparator) {
        this.comparator = comparator;
    }



    public void add(E element) {
        elementNotNullCheck(element);
        //添加第一个节点
        if (root == null) {
            root = createNode(element, null);
            size++;
            //新添加节点之后的处理
            afterAdd(root);
            return;
        }
        //添加的不是第一个节点
        //找到父节点
        Node<E> parent = root;
        Node<E> node = root;
        int cmp = 0;
        while (node != null) {
            cmp = compare(element, node.element);
            parent = node;
            if (cmp > 0) {
                node = node.right;
            } else if (cmp < 0) {
                node = node.left;
            } else {
                //相等
                node.element = element;
                return;
            }
        }
        //查到父节点的位置
        Node<E> newNode = createNode(element, parent);
        if (cmp > 0) {
            parent.right = newNode;
        } else {
            parent.left = newNode;
        }
        size++;
        //新添加节点之后的处理
        afterAdd(newNode);

    }

    protected void afterAdd(Node<E> node) {

    }

    public void remove(E element) {
        remove(node(element));
    }

    private void remove(Node<E> node) {
        if (node == null) return;
        size --;


        //度为2的节点
        if (node.hasTwoChildren()) {
            //找到后继节点
            Node<E> s = successor(node);
            //用后继节点的值覆盖度为2的值
            node.element = s.element;
            //删除后继节点
            node = s;
        }

        //删除node节点 node的度必为1或者0
        Node<E> replacement = node.left != null ? node.left : node.right;
        //node是度为1的节点
        if (replacement != null) {
            //更改parent
            replacement.parent = node.parent;
            //更改parent的left,right
            if (node.parent == null) {
                root = replacement;
            } else if (node == node.parent.left) {
                node.parent.left = replacement;
            } else {
                node.parent.right = replacement;
            }
            //恢复平衡 (删除节点之后的处理)
            afterRemove(node);


        } else if (node.parent == null) {//node是叶子节点并且是根节点
            root = null;

            //恢复平衡 (删除节点之后的处理)
            afterRemove(node);
        } else { //是叶子节点，但不是根节点
            if (node == node.parent.left) {
                node.parent.left = null;
            } else {
                node.parent.right = null;
            }

            //恢复平衡 (删除节点之后的处理)
            afterRemove(node);
        }
    }

    protected void afterRemove(Node<E> node) {}

    private Node<E> node(E element) {
        Node<E> node = root;
        while (node != null) {
            int cmp = compare(element, node.element);
            if (cmp == 0) return node;
            if (cmp > 0) {
                node = node.right;
            } else {
                node = node.left;
            }
        }
        return null;
    }

    public boolean contains(E element) {
        return node(element) != null;
    }



    /**
     * @param e1
     * @param e2
     * @return 0 e1==e2 >0 e1>e2 <0 e1<e2
     */
    public int compare(E e1, E e2) {
        if (comparator != null) {
            return comparator.compare(e1, e2);
        }
        return ((Comparable<E>) e1).compareTo(e2);
    }



    private void elementNotNullCheck(E element) {
        if (element == null) {
            throw new IllegalArgumentException("element must not be null");
        }
    }
}
